package com.tl.web05.controller;

import com.alibaba.fastjson.JSONObject;
import com.tl.web05.config.Config;
import com.tl.web05.constant.ResponseResult;
import com.tl.web05.dao.UserDao;
import com.tl.web05.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AjaxDelServlet", urlPatterns = "/ajax_del")
public class DelUserServlet extends HttpServlet {
    private UserService userService = new UserService(new UserDao());

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //配置响应编码
//            request.setCharacterEncoding(Config.REQ_ChARSET);
            response.setContentType(Config.RESP_JSON_CONTENT_TYPE);
            //获取参数
            String username = request.getParameter("username");
            //从map中删除该元素
//            ServletContext application = request.getServletContext();
//            HashMap<String, User> userMap = (HashMap<String, User>) application.getAttribute(Config.USER_MAP);
//            ResponseResult responseResult = userService.delUser(userMap, username);
            //从数据库中删除
            ResponseResult responseResult = userService.delUser(username);
            //响应结果
            response.getWriter().print(JSONObject.toJSONString(responseResult));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
